import Vue from 'vue'
import router from '@/router'
import store from '@/store'
import request from './httpRequest'
/**
 * 获取uuid
 */
export function getUUID () {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
  })
}

/**
 * 是否有权限
 * @param {*} key
 */
export function isAuth (key) {
  return JSON.parse(sessionStorage.getItem('permissions') || '[]').indexOf(key) !== -1 || false
}

/**
 * 树形数据转换
 * @param {*} data
 * @param {*} id
 * @param {} pid
 */
export function treeDataTranslate (data, id = 'id', pid = 'parentId') {
  var res = []
  var temp = {}
  for (var i = 0; i < data.length; i++) {
    temp[data[i][id]] = data[i]
  }
  for (var k = 0; k < data.length; k++) {
    if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
      if (!temp[data[k][pid]]['children']) {
        temp[data[k][pid]]['children'] = []
      }
      if (!temp[data[k][pid]]['_level']) {
        temp[data[k][pid]]['_level'] = 1
      }
      data[k]['_level'] = temp[data[k][pid]]._level + 1
      temp[data[k][pid]]['children'].push(data[k])
    } else {
      res.push(data[k])
    }
  }
  return res
}

/**
 * 清除登录信息
 */
export function clearLoginInfo () {
  Vue.cookie.delete('access-token')
  store.commit('resetStore')
  router.options.isAddDynamicMenuRoutes = false
}

// 换肤加class函数
export function toggleClass (element, className) {
  if (!element || !className) {
    return
  }
  element.className = className
}

export function addStyle (dom, obj) {
  if (!dom || !obj) return
  for (let prop in obj) {
    dom.style[prop] = obj[prop]
  }
}

/**
 * 获取激活顶部菜单名
 * 有这个菜单名来判断顶部改选择哪个
 */
export function headerMenuActiveName (_this) {
  // 以 / 开始  [A-Za-z0-9_-]  以 / 或没有 / 结尾
  let name = /^\/[\w|-]+(\/)?/.exec(_this.$route.path)[0]
  return name.includes('/', 1) ? name.slice(1, -1) : name.slice(1)
}
/**
 * 根据社区ID获取社区信息
 * @param {*} id
 */
export function getHousingEstate (id) {
  return request({
    url: `/dg-ic/ic/housing-estate/info/${id}`,
    method: 'get',
    params: request.adornParams()
  })
}
/**
* 根据code获取数据字典列表
* 例：this.dateFormat('Dec 27, 2017 3:18:14 PM') 得到 '2017-12-27 15:18:14'
*/
export function getDictList (code) {
  return request({
    // url: request.adornUrl(`/common/dgbasedictionary/dictlist/${code}`),
    url: `/dg-standard-info/common/dgbasedictionary/dictlist/${code}`,
    method: 'get',
    params: request.adornParams()
  })
}
/**
* 根据人员ID获取信息详情
*/
export function getPopulationMessage (id) {
  return request({
    // url: request.adornUrl(`/standardinfo/dgactualpopulation/info/${id}`),
    url:`/dg-standard-info/standardinfo/dgactualpopulation/info/${id}`,
    method: 'get',
    params: request.adornParams()
  })
}
/**
* 根据单位ID获取信息详情
*/
export function getUnitMessage (id) {
  return request({
    // url: request.adornUrl(`/standardinfo/dgactualunit/info/${id}`),
    url: `/dg-standard-info/standardinfo/dgactualunit/info/${id}`,
    method: 'get',
    params: request.adornParams()
  })
}
/**
* 检查ID是不是存在
*/
export function existIdCard (idcard,id) {
  return request({
    // url: request.adornUrl(`/standardinfo/dgactualpopulation/existIdCard/${idcard}/${id}`),
    url: `/dg-standard-info/dgactualpopulation/existIdCard/${idcard}/${id}`,
    method: 'get',
    params: request.adornParams()
  })
}
/**
* 根据人员身份证获取详细信息
*/
export function existPopulationByIdCard (idcard) {
  return request({
    // url: request.adornUrl(`/standardinfo/dgactualpopulation/existIdCard/${idcard}/${id}`),
    url: `/dg-standard-info/dgactualpopulation/infobyidcard/${idcard}`,
    method: 'get',
    params: request.adornParams()
  })
}
/**
* 根据图像id获取图像详情
*/
export function getPhotoUrl (photoId) {
  return request({
    // url: request.adornUrl(`/standardinfo/dgbasicportrait/info/${photoId}`),
    url: `/dg-standard-info/standardinfo/dgbasicportrait/info/${photoId}`,
    method: 'get',
    params: request.adornParams()
  })
}
/**
* 检查地址参数是不是存在
*/
export function checkAddress (address) {
  return request({
    url: `/dg-standard-info/dgstandardaddress/checkaddress`,
    method: 'post',
    params: request.adornParams(address)
  })
}
